package com.funever.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import net.tsz.afinal.annotation.view.ViewInject;
import com.funever.combworkalerm.R;
import com.funever.entity.WorkType;
import com.funever.fragment.BaseFragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class AddTypeActivity extends BaseFragment{
	
	@ViewInject(id=R.id.type_name)
	private EditText type_name;
	@ViewInject(id=R.id.st_1,click="onClick")
	private EditText st_1;
	@ViewInject(id=R.id.st_2,click="onClick")
	private EditText st_2;
	@ViewInject(id=R.id.et_1,click="onClick")
	private EditText et_1;
	@ViewInject(id=R.id.et_2,click="onClick")
	private EditText et_2;
	@ViewInject(id=R.id.submit,click="onClick")
	private Button submit;
	TextView nv_title;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		mainView = inflater.inflate(R.layout.add_type, null);
		return mainView;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		getActivity().setTitle("添加班次类型");
	}
	
	public void onClick(View v){
		if(v == st_1||v == et_1||v == st_2||v == et_2){
			EditText et = (EditText) v;
			showTimePicker(et,"12:00");
		}else if(v == submit){
			if(type_name.getText().toString().equals("")){
				Toast.makeText(getActivity(), "取一个班名吧!", Toast.LENGTH_LONG).show();
				return;
			}
			WorkType wt = new WorkType();
			wt.setName(type_name.getText().toString().toUpperCase());
			wt.setStartTime1(st_1.getText().toString());
			wt.setEndTime1(et_1.getText().toString());
			wt.setEndTime2(et_2.getText().toString());
			
			List<WorkType> wl = WorkType.findAllByWhere("name='"+wt.getName()+"'");
			if(wl.size()>0){
				Toast.makeText(getActivity(), "已经存在了这个班名!", Toast.LENGTH_LONG).show();
				return;
			}else{
				wt.save();
				Toast.makeText(getActivity(), "添加成功！", Toast.LENGTH_LONG).show();
			}
		}
	}
	
	/**
	 * 显示选择Time的dialog
	 */
	public void showTimePicker(final EditText et,String strDate){
		final Calendar calendar = Calendar.getInstance();
		final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
		Date date = null;
		try {
			date = sdf.parse(strDate);
			calendar.setTime(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		TimePickerDialog tpd = new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); 
				calendar.set(Calendar.MINUTE, minute); 
				et.setText(sdf.format(calendar.getTime()));
			}
		}, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
		tpd.show();
	}
}
